define(['jquery', 'bootstrap','backend', 'frontend', 'form', 'template','table'], function ($, undefined,Backend, Frontend, Form, Template,Table) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {

        domain:function () {

            Table.api.init({
                extend: {
                    index_url: index_url+ location.search,
                    // add_url: 'domain/add',
                    // edit_url: 'domain/edit',
                    del_url: del_url
                    // multi_url: 'domain/multi',
                    // table: 'main_table',
                }
            });

            var main_table = $("#main_table");
            var sub_table = $("#sub_table");

            //后缀z是中转  l是落地域名。
            $(".btn_add_z").on("click",function () {
                Layer.open({
                    content: "<textarea id=\"domains\" data-rule=\"required\" style=\"width: 500px;height: 100px\" placeholder=\"请将要添加的域名，输入或者复制到这里，每行一个。不带http,请不要直接使用一级域名\"></textarea>",
                    area: ['600px', '280px'],
                    title: '中转域名',
                    btn: ['确定', '取消'],
                    yes: function(){
                        var domains = $('#domains').val();
                        Fast.api.ajax({
                            url: add_url,
                            data: {'domains':domains, "uid":uid, "is_main_switch":1}
                        }, function (data, ret) {
                            Layer.msg('中转域名保存成功');
                            Layer.close();
                            main_table.bootstrapTable('refresh');
                        });
                    },
                    btn2: function(){
                        return 0;
                    }
                });
            });

            $(".btn_add_l").on("click",function () {
                Layer.open({
                    content: "<textarea id=\"domains\" data-rule=\"required\" style=\"width: 500px;height: 100px\" placeholder=\"请将要添加的域名，输入或者复制到这里，每行一个。不带http,请不要直接使用一级域名\"></textarea>",
                    area: ['600px', '280px'],
                    title: '中转域名',
                    btn: ['确定', '取消'],
                    yes: function(){
                        var domains = $('#domains').val();
                        Fast.api.ajax({
                            url: add_url,
                            data: {'domains':domains, "uid":uid, "is_main_switch":0}
                        }, function (data, ret) {
                            Layer.close();
                            sub_table.bootstrapTable('refresh');
                            Layer.msg('落地域名保存成功');
                        });
                    },
                    btn2: function(){
                        return 0;
                    }
                });
            });

            // 初始化表格
            main_table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams:function (params) {
                    params.filter = JSON.stringify({is_main_switch:1,uid:uid});
                    params.op = JSON.stringify({is_main_switch:'=',uid:'='});
                    return params;},
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title:'ID'},
                        {field: 'domain', title: '域名'},
                        {field: 'status', title: '状态'},
                        {field: 'create_time', title: '创建时间', operate:'RANGE', addclass:'datetimerange'},
                        {field: 'update_time', title: '更新时间', operate:'RANGE', addclass:'datetimerange'},
                        {field: 'operate', title: '操作', table: main_table, events: Table.api.events.operate,
                            buttons: [{
                                    name: 'repeat',
                                    icon: 'fa fa-repeat',
                                    classname: 'btn btn-info btn-xs btn-execute btn-ajax',
                                    url: repeat_url,
                                    confirm: "确认重置状态吗？",
                                    success: function (data) {
                                        main_table.bootstrapTable('refresh');
                                        return false;
                                    }
                                },
                                {
                                    name: 'ban',
                                    icon: 'fa fa-ban',
                                    classname: 'btn btn-info btn-xs btn-execute btn-ajax',
                                    url: ban_url,
                                    confirm: "要禁用这个域名吗？",
                                    success: function (data) {
                                        main_table.bootstrapTable('refresh');
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            sub_table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams:function (params) {
                    params.filter = JSON.stringify({is_main_switch:0,uid:uid});
                    params.op = JSON.stringify({is_main_switch:'=',uid:'='});
                    return params;},
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title:'ID'},
                        {field: 'domain', title: '域名'},
                        {field: 'status', title: '状态'},
                        {field: 'create_time', title: '创建时间', operate:'RANGE', addclass:'datetimerange'},
                        {field: 'update_time', title: '更新时间', operate:'RANGE', addclass:'datetimerange'},
                        {field: 'operate', title: '操作', table: sub_table, events: Table.api.events.operate,
                            buttons: [{
                                    name: 'repeat',
                                    icon: 'fa fa-repeat',
                                    classname: 'btn btn-info btn-xs btn-execute btn-ajax',
                                    url: repeat_url,
                                    confirm: "确认重置状态吗？",
                                    success: function (data) {
                                        sub_table.bootstrapTable('refresh');
                                        return false;
                                    }
                                },
                                {
                                name: 'ban',
                                icon: 'fa fa-ban',
                                classname: 'btn btn-info btn-xs btn-execute btn-ajax',
                                url: ban_url,
                                confirm: "要禁用这个域名吗？",
                                success: function (data) {
                                    sub_table.bootstrapTable('refresh');
                                    return false;
                                }
                            }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(main_table);
            Table.api.bindevent(sub_table);

        },

        gzhdomain:function () {
            $(".gzhverifyfile").on("click",function () {
                var verifyfile = $("#verifyfile").val();
                console.log(verifyfile); //MP_verify_2trCUtxRxycJqfNA.txt
                if (verifyfile && verifyfile.startsWith('MP_verify_') && verifyfile.endsWith('.txt')) {
                    Fast.api.ajax({
                        url: gzhverifyfile_url,
                        data: {domain:$("#verifyfile").val(), 'uid':uid},
                    }, function (data, ret) {
                        console.log("文件保存成功");
                        Layer.msg('文件保存成功');
                    });
                } else Layer.msg('文件名格式不对');
            });
            $(".adddomain").on("click",function () {
                var gzhdomain = $("#gzhdomain").val();
                console.log(gzhdomain);
                if (gzhdomain && (gzhdomain.split('.').length - 1) > 1) {
                    Fast.api.ajax({
                        url: addgzhdomain_url,
                        data: {domain:gzhdomain},
                    }, function (data, ret) {
                        Layer.msg('保存成功');
                    });
                } else Layer.msg('域名不对');
            });

            $(".saveAppId").on("click",function () {
                var appid = $("#gzhappid").val();
                console.log(appid);
                if (appid) {
                    Fast.api.ajax({
                        url: saveappid_url,
                        data: {domain:appid},
                    }, function (data, ret) {
                        Layer.msg('保存成功');
                    });
                } else Layer.msg('请输入appid');
            });
            $(".saveSecret").on("click",function () {
                var appSecret = $("#gzhAppSecret").val();
                console.log(appSecret);
                if (appSecret) {
                    Fast.api.ajax({
                        url: saveappsecret_url,
                        data: {domain:appSecret},
                    }, function (data, ret) {
                        Layer.msg('保存成功');
                    });
                } else Layer.msg('请输入AppSecret');
            });

        }
    };

    return Controller;
});